package com.dlwx.firefighting.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.dlwx.firefighting.R;
import com.dlwx.firefighting.adapter.ProductAdapter;
import com.dlwx.firefighting.bean.BaseBean;
import com.dlwx.firefighting.bean.GoodsBean;
import com.dlwx.firefighting.config.Constant;
import com.dlwx.firefighting.utils.DealResult;
import com.dlwx.firefighting.utils.LogUtils;
import com.dlwx.firefighting.utils.ToastUtils;
import com.dlwx.firefighting.views.GridSpacingItemDecoration;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.github.jdsjlzx.view.CommonHeader;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ProductListActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.tv_filter)
    TextView tvFilter;
    @BindView(R.id.recyclerView)
    LRecyclerView recyclerView;
    private ArrayList<String> titleList = new ArrayList<String>() {{
        add("全部");
        add("消防工程");
        add("消防工程");
        add("消防工程");
        add("消防工程");
        add("消防工程");
    }};
    private ProductAdapter mAdapter;
    private LRecyclerViewAdapter lRecyclerViewAdapter;
    private int pid;
    private String title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_list);
        ButterKnife.bind(this);
        pid=getIntent().getIntExtra("id",0);
        title=getIntent().getStringExtra("name");
        initView();
        getDatas();
    }

    private void getDatas() {
        OkGo.<String>post(Constant.plist)
                .tag(this)
                .params("pid", pid)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        List<GoodsBean> beans = DealResult.getInstance().dealDatas(ProductListActivity.this, response, new TypeToken<BaseBean<GoodsBean,GoodsBean>>() {
                        }.getType());
                        mAdapter.addAll(beans);
                        recyclerView.refreshComplete(mAdapter.getItemCount());
                    }
                    @Override
                    public void onError(Response<String> response) {
                        ToastUtils.toastForShort(ProductListActivity.this, getString(R.string.bad_net));
                        super.onError(response);
                    }
                });
    }

    private void initView() {
        tvMiddleTitle.setText(title);
        tabLayout.addTab(tabLayout.newTab().setText("消防工程"));
        tabLayout.addTab(tabLayout.newTab().setText("消防工程"));
        tabLayout.addTab(tabLayout.newTab().setText("消防工程"));
        tabLayout.addTab(tabLayout.newTab().setText("消防工程"));
        tabLayout.addTab(tabLayout.newTab().setText("消防工程"));
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                LogUtils.i(tab.getPosition()+"");
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
//        
        mAdapter = new ProductAdapter(this);
        recyclerView.setLayoutManager(new GridLayoutManager(this,2));
        mAdapter.setDataList(new ArrayList<GoodsBean>());
        lRecyclerViewAdapter = new LRecyclerViewAdapter(mAdapter);
        recyclerView.setAdapter(lRecyclerViewAdapter);
        //divider
        GridSpacingItemDecoration gridSpacingItemDecoration=new GridSpacingItemDecoration(2,20,true);
        recyclerView.setHasFixedSize(true);
        recyclerView.addItemDecoration(gridSpacingItemDecoration);
        recyclerView.setPullRefreshEnabled(true);
        recyclerView.setLoadMoreEnabled(true);
        addHeader();
    }

    private void addHeader() {
        CommonHeader header = new CommonHeader(this, R.layout.header_line);
        lRecyclerViewAdapter.addHeaderView(header);
    }
    @OnClick({R.id.title_left_one_btn, R.id.tv_middle_title, R.id.tv_filter})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                this.finish();
                break;
            case R.id.tv_middle_title:
                break;
            case R.id.tv_filter:
                break;
        }
    }
}
